<?php

namespace app\api\controller\gifts;

use app\api\controller\Controller;
use app\api\model\gifts\UserGifts as UserGiftsModel;
use think\Request;

/**
 * 用户爆品礼
 * Class UserGifts
 * @package app\api\controller
 */
class UserGifts extends Controller
{
    /* @var UserGiftsModel $model */
    protected $model;

    /* @var \app\api\model\User $model */
    protected $user;

    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new UserGiftsModel;
        $this->user = $this->getUser();
    }

    /**
     * 购买的爆品礼列表
     * @param string $data_type
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function buyLists()
    {
        $param = Request::instance()->param();
        $list = $this->model->buyLists($param,$this->user['user_id']);
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 领取的爆品礼列表
     * @param string $data_type
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function receiveLists()
    {
        $param = Request::instance()->param();
        $list = $this->model->receiveLists($param,$this->user['user_id']);
        return $this->renderSuccess(compact('list'));
    }


    /**
     * 领取爆品
     * @param $gifts_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function receive()
    {
        $param = Request::instance()->param();
        if($this->user['shareholder']==0){
            return $this->renderError('您还不是微股东,无领取权限');
        }
        if ($this->model->receive($this->user, $param['gifts_id'],$param['superior_user_id'])) {
            return $this->renderSuccess([], '领取成功');
        }
        return $this->renderError($this->model->getError() ?: '添加失败');
    }


    /**
     * 已经领取爆品礼详情
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function getGiftsDetail()
    {
        $param = Request::instance()->param();
        $model = UserGiftsModel::isGetDetail($param['user_gifts_id']);
        if(empty($model)){
            return $this->renderError('爆品礼不存在，请刷新后重试');
        }
        $model['gifts_content'] = htmlspecialchars_decode($model['gifts_content']);
        return $this->renderSuccess($model);
    }


    /**
     * 已经购买的爆品礼详情
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function buyGiftsDetail()
    {
        $param = Request::instance()->param();
        $model = UserGiftsModel::buyGetDetail($param['user_gifts_id']);
        if(empty($model)){
            return $this->renderError('爆品礼不存在，请刷新后重试');
        }
        $model['gifts_content'] = htmlspecialchars_decode($model['gifts_content']);
        return $this->renderSuccess($model);
    }



}